from google.appengine.ext import db
from google.appengine.ext.db import polymodel
import gravatar

class Person(polymodel.PolyModel):
	nick = db.StringProperty(required=True)
	name = db.StringProperty(required=True)
	surname = db.StringProperty(required=True)
	born_on = db.DateProperty()
	user = db.UserProperty(auto_current_user_add=True)
	mobile = db.PhoneNumberProperty()
	address = db.PostalAddressProperty()
	def gravatar_url(self, size=100): return gravatar.url(email=self.user.email(), default='monsterid', size=size) 
	def __unicode__(self): return u"%s %s" % (self.name, self.surname)

class Room(polymodel.PolyModel):
	floor = db.IntegerProperty()
	building = db.StringProperty()

class CommonRoom(Room):
	name = db.StringProperty(required=True)
	description = db.TextProperty()
	def __unicode__(self): return unicode(name)

class StudentRoom(Room):
	number = db.IntegerProperty(required=True)

class Year(db.Model):
	number = db.IntegerProperty(required=True)
	startdate = db.DateProperty()
	enddate = db.DateProperty()
	def __unicode__(self): return unicode('%s/%s' % (self.startdate.year, self.enddate.year))

class GalileanClass(db.Model):
	db.CategoryProperty(required=True, choices=set(('naturali','morali')))

class Student(Person):
	active = db.BooleanProperty(required=True, default=True)
	year = db.ReferenceProperty(Year)
	galileianclass = db.ReferenceProperty(GalileanClass)
	subject = db.CategoryProperty()
	home_phone = db.PhoneNumberProperty()

class Professor(Person):
	subject = db.CategoryProperty()
	abstract = db.TextProperty()
	office_phone = db.PhoneNumberProperty()

class GalileianShip(db.Model):
	student = db.ReferenceProperty(Student)
	year = db.ReferenceProperty(Year)

class TutorShip(db.Model):
	professor = db.ReferenceProperty(Professor)
	year = db.ReferenceProperty(Year)

class Course(db.Model):
	year = db.ReferenceProperty(Year)
	professor = db.ReferenceProperty(Professor)
	name = db.StringProperty()
	abstract = db.TextProperty()
	cfu = db.IntegerProperty(default=4)

class CourseAttendance(db.Model):
	course = db.ReferenceProperty(Course)
	student = db.ReferenceProperty(Student)

class Event(polymodel.PolyModel):
	start = db.DateTimeProperty(required=True)
	end = db.DateTimeProperty()
	location = db.ReferenceProperty(Room)
